
#include <iostream>
using  namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/**
 * 取绝对值
 * @param x
 * @return
 */
int abs_72(int x){
    return x >= 0 ? x : -x;
}
/**
 * 取较大值
 * @param x
 * @param y
 * @return
 */
int max_72(int x, int y){
    return x > y ? x : y;
}
int min_72(int x, int y){
    return x <  y ? x : y;
}
/**
 * 二叉树的高度
 * @param node
 * @return
 */
int get_height(TreeNode * node){
    if(node == NULL){
        return 0;
    }
    return max_72(1+get_height(node->left), 1+ get_height(node->right));
}

/**
* 判断是否是平衡二叉树
* @param root
* @return
*/
bool isBalanced(TreeNode* root) {
    if(root == NULL){
        return true;
    }
    return abs_72(get_height(root->left)-get_height(root->right)) <=1 && isBalanced(root->left) && isBalanced(root->right);
}

int main72(){
    TreeNode node1 = {1};
    TreeNode node2 = {3};
    TreeNode node3 = {2};

    node1.right = &node2;

    node2.left = &node3;
    cout << isBalanced(&node1);
    return 0;
}


